package com.learn.UnSafeCollection;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

//  解决并发修改异常 java.util.ConcurrentModificationException !!

public class MapTest {

    public static void main(String[] args) {

        /**
         * 同理:Map map = new HashMap();  不安全
         * 1.Map map = Collections.synchronizedMap(new HashMap<>());
         * 2.Set set = new CopyOnWriteArraySet();
         *
         * 由此可以引申出 什么是HashSet?
         * 其实就是HashSet底层就是new HasmMap()
         * 那么什么是HashMap  牢记( 负载因子0.75 和 初始值16)
         */
        Map map = new ConcurrentHashMap();
        for (int i = 1; i <= 20; i++) {
            new Thread(()->{
                String substring = UUID.randomUUID().toString().substring(0,5);
                map.put(UUID.randomUUID().toString().substring(1,3),substring);
                System.out.println(map);
            },String.valueOf(i)).start();
        }
    }
}
